<?php

class GoodsAction extends BaseAction{
	
		public function index() {
			
			/**用来处理带有条件的查询**/
			$partner_id = $_REQUEST['partner_id'];
			$sort_id = $_REQUEST['sort_id'];
			$id = $_REQUEST['good_id'];
			$enable = $_REQUEST['enable'];
			if(empty($enable)){
				$enable='all';
			}
			$map=' 1=1 ';
			if(!empty($partner_id)){
				$map=$map.' and ( partner_id ='.$partner_id. ' ) ';
			}
			if(!empty($sort_id)){
				$map=$map." and ( sort_id ='".$sort_id."' ) ";
			}
			if (!empty($id)){
				$map=$map.' and ( id ='.$id.')';
			}
			if ($enable!='all'){
				$map=$map.' and ( enable = "'.$enable.'")';
			}				
							
			$pagedata = D('Goods')->selectAllByPage($map);
			$this->assignRequestParams();
			$this->assign('pagedata', $pagedata);
			$this->display();
		}
		
		
		public function add() {
			$this -> display("edit");
		}
		
		public function editGood() {
			
			$id = $_GET['id'];
			if(empty($id))$this->error('参数错误！');
			$good = M('Goods')->where('id='.$_GET['id'])->find();
			if(empty($good))$this->error('商品不存在！');
			
			$listprice = D('GoodsPrice')->where('goods_id='.$id) ->find();
			$good['listprice'] = $listprice;			
			
			$this->assign('type', 'edit');
			$this->assign('good', $good);
			
	
			$this->display('edit');
		}
		
		public function doAddGood(){
			
			
			$map['title'] = t($_POST['title']);
			$map['summary'] = t($_POST['summary']);
			$map['detail'] = stripslashes($_POST['detail']);
			$map['sort_id'] = $_POST['sort_id'];
			$map['partner_id'] = intval($_POST['partner_id']);
			$map['send_number'] = 0;
			$map['total_number'] = 0;
			$map['expire_time'] = paramDate($_POST['expire_time']);
			$map['start_time'] = paramDate($_POST['start_time']);
			//$map['explain'] = $_POST['explain'];
			if($map['start_time']>=$map['expire_time'])$this->error('开始时间需小于截止时间！');
			
			if(isset($_POST['enable']))
				$map['enable'] = 'Y';
			else
				$map['enable'] = 'N';
				
			$map['get_type'] = $_POST['get_type'];
			if(!in_array($map['get_type'],array('self','delivery','all')))$map['get_type'] = 'self';
			$map['is_delete'] = 'N';
			$map['ctime'] = time();
			$map['mtime'] = time();
			$map['insert_admin_id'] 	=	$this->adminId;
		
			$map ['goods_icon'] = 'default.gif';
			
			$Goods = D ( 'Goods' );
			$id = $Goods ->add ( $map );
			unset ( $map );
			
			//上传图标附件
			if(!empty($id)){
	    	$options =	array();
			 	$options['userId']			=	$this->adminId;
				$options['allow_exts']	=	'jpg,gif,jpeg,bmp';
				$options['custom_path'] = 'goods/'.$id.'/';
				$options['save_path']	=	UPLOAD_PATH.'/'.$options['custom_path'];
				$info	=	X('Xattach')->upload('goods',$options);
				
				if($info['status']) {
						$map['goods_icon'] = $info['info'][0]['savepath'].$info['info'][0]['savename'];
						$map['id'] = $id;
						$Goods->save($map);
						
				}else{
				}
				
				$this->assign('jumpUrl',U('/Goods/index'));
				$this->success('新增成功！');
			}else{
				$this->error('新增失败！');				
			}
		}
		
		
		public function doEditGood(){
			
			$id = intval($_POST['id']);			
			if(empty($id))$this->error('参数错误！');
			$map['id'] = $id;
			$map['title'] = t($_POST['title']);
			$map['summary'] = t($_POST['summary']);
			$map['detail'] = stripslashes($_POST['detail']);
			$map['sort_id'] = $_POST['sort_id'];
			$map['partner_id'] = intval($_POST['partner_id']);
			$map['expire_time'] = paramDate($_POST['expire_time']);
			$map['start_time'] = paramDate($_POST['start_time']);
			//$map['explain'] = $_POST['explain'];
			if($map['start_time']>=$map['expire_time'])$this->error('开始时间需小于截止时间！');

			if (isset ( $_POST ['enable'] ))
				$map ['enable'] = 'Y';
			else
				$map ['enable'] = 'N';
			$map['get_type'] = $_POST['get_type'];
			if(!in_array($map['get_type'],array('self','delivery','all')))$map['get_type'] = 'self';
			
			$map['mtime'] = time();
			$Goods = D ( 'Goods' );
			$Goods->save($map);

			unset ( $map );
			//上传图标附件
			if(!empty($id)){
	    	$options =	array();
			 	$options['userId']			=	$this->adminId;
				$options['allow_exts']	=	'jpg,gif,jpeg,bmp';
				$options['custom_path'] = 'goods/'.$id.'/';
				$options['save_path']	=	UPLOAD_PATH.'/'.$options['custom_path'];
				$info	=	X('Xattach')->upload('goods',$options);
				
				if($info['status']) {
						$map['goods_icon'] = $info['info'][0]['savepath'].$info['info'][0]['savename'];
						$map['id'] = $id;
						$Goods->save($map);
						
				}else{
				}
				
				$this->assign('jumpUrl',U('/Goods/index'));
				$this->success('修改成功！');
			}else{
				$this->error('修改失败！');				
			}
		
			
		}		
		
		
		public function doDeleteGood() {
			$_POST['ids']	= explode(',', t($_POST['ids']));
			if (empty($_POST['ids'])) {
				echo 0;
				return ;
			}

			$deleted = D('Goods')->deleteByIds($_POST['ids']);
			echo $deleted ? '1' : '0';

		}
		
		
		public function getJson(){
				$searchtext = $_REQUEST['searchtext'];
				$partner_id = $_REQUEST['partner_id'];
				
				$map = ' 1=1 ';
				if(!empty($partner_id)){
						$map=$map." and partner_id=".$partner_id;
				}
				if(!empty($searchtext)){
						$map=$map." and title like '%".$searchtext."%' ";
				}
				$listpartner = D('Goods')->selectAllByPage($map,100);
				exit(json_encode($listpartner['data'])); 
		}
		
		
		public function imgEdit(){
			$id = $_GET['id'];
			$type = $_GET['type'];
			$newimgpath = $_POST['newimgpath'];
			if(!empty($newimgpath)){
				$result['status']=0;
				if(empty($id))exit(json_encode($result)); 
				if(empty($type))exit(json_encode($result)); 
				$map['id']=$id;
				if($type=='logo')$map['goods_icon']=$newimgpath;
				//if($type=='intro1')$map['intro_imgpath1']=$newimgpath;
				if(empty($map['goods_icon']))exit(json_encode($result));
				$no = M('Goods')->save($map);
				if($no)$result['status']=1;
				exit(json_encode($result));
			}
			if(empty($id))$this->error('参数错误！');
			$goods = M('Goods')->where('id='.$_GET['id'])->find();
			if(empty($goods))$this->error('商品不存在！');
			$this->assign('goods', $goods);
			$this->display();
		}
}


?>